//
//  UIView+Extend.m
//  chijidun
//
//  Created by iMac on 16/8/27.
//
//

#import "UIView+Extend.h"


@implementation UIView (Extend)


-(void)showHudWith:(NSString *)title
{
    UIImageView *backView=[[UIImageView alloc]initWithFrame:self.bounds];
    [backView setImage:[UIImage imageNamed:@"login_warnback"]];
    [self addSubview:backView];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 240, 50)];
    [label setCenter:CGPointMake(kMainScreen_Width*0.5, kMainScreen_Height*0.4)];
    [label.layer setBackgroundColor:WHITE_COLOR.CGColor];
    label.layer.cornerRadius=29.0f;
    [label setTextColor:MAIN_COLOR];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [backView addSubview:label];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [backView removeFromSuperview];
    });
}
-(void)addShadow
{
    float border=3.0f;
    self.layer.shadowOpacity = 0.3;
    self.layer.shadowColor = LIGHT_COLOR.CGColor;
    self.layer.shadowRadius = border;
    self.layer.cornerRadius=2.0f;
    self.layer.shadowOffset = CGSizeMake(0,0);
    
    float width = self.bounds.size.width;
    float height = self.bounds.size.height;
    
    UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(-border, -border, width+2*border, height+2*border)];
    aPath.lineWidth = border;
    aPath.lineCapStyle = kCGLineCapRound;
    aPath.lineJoinStyle = kCGLineCapRound;
    self.layer.shadowPath = aPath.CGPath;
}
-(void)addShadowWith:(CGSize)size
{
    
    float border=3.0f;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowColor = LIGHT_COLOR.CGColor;
    self.layer.shadowOffset = CGSizeMake(0,0);
    self.layer.shadowRadius = border;
    self.layer.cornerRadius=2.0f;
    
    float width = size.width;
    float height = size.height;
    
    UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(-border, -border, width+2*border, height+2*border)];
    aPath.lineWidth = border;
    aPath.lineCapStyle = kCGLineCapRound;
    aPath.lineJoinStyle = kCGLineCapRound;
    self.layer.shadowPath = aPath.CGPath;
}
- (void)showHUD:(NSString *)title {
    [self showHUD:title callback:nil];
}

- (void)showHUD:(NSString *)title callback:(MBProgressHUDCompletionBlock)callback {
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self];
    hud.label.text = title;
    hud.label.font =[UIFont LightFontOfSize:15];
    hud.mode = MBProgressHUDModeCustomView;
    [self addSubview:hud];
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:1];
    hud.completionBlock = callback;
}

-(void)showHUDDesc:(NSString *)desc
{
    [self showHUDDesc:desc callback:nil];
}
- (void)showHUDDesc:(NSString *)desc callback:(MBProgressHUDCompletionBlock)callback {
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self];
    hud.detailsLabel.font=[UIFont LightFontOfSize:15];
    hud.detailsLabel.text  = desc;
    hud.mode = MBProgressHUDModeCustomView;
    [self addSubview:hud];
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:1];
    hud.completionBlock = callback;
}

- (void)click:(id)target action:(SEL)action {
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:action]];
}

-(void)callPhoneWith:(NSString *)tele
{
    NSString *urlString = [NSString stringWithFormat:@"tel://%@",tele];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
}

@end







